<?php
namespace Admin\Controller;

class UserController extends AdminController {
    
   
     /**
     * 用户管理首页
     * @return none
     */
    public function index($username='', $usertype=-1){
        $where = 'usertype!=2';
        if(trim($username) != ''){
            $where .= ' and username like \'%' .$username . '%\'';
        }
        if($usertype != -1){
            $where .= ' and usertype=' .$usertype;
        }
        $page = page("User", 10, $where, array('id' => 'desc'));
        $this->assign($page);
        $this->display();
    }

     /**
     * 添加
     * @return none
     */
    public function addUser($username='', $password='', $usertype=2){
        if(IS_POST){
            $user = array(
                'username' => $username,
                'password' => md5($password),
                'usertype' => 2,
                'status'   => 1,
            );
            if($uid = M('User')->add($user)){
                M('Auth_group_access')->add(array('uid' => $uid, 'group_id' => '3'));
                $this->success('添加成功');
            }else{
               $this->error('错误');
            }          
        }else{
            $this->display();
        }
    }
    
    /**
     * 删除用户
     * @param type $id
     * @param type $usertype
     */
    public function delUser($id=0, $usertype=0){
        M('User')->delete($id);
        if($usertype == 1){
            $seller = M('User')->field('third_id')->where(array('id' => $id))->find();
            M('User_third')->where(array('id' => $seller['third_id']))->delete();
            M('User_seller')->where(array('uid' => $id))->delete();
        }else{
            M('User_model')->where(array('uid' => $id))->delete();
            $weibo = M('Weibo')->where(array('uid' => $id))->select();
            foreach ($weibo as $key => $value){
                $pics = M('Weibo_pic')->where(array('w_id' => $value['id']))->select();
                foreach ($pics as $k => $v) {
                    unlink('.' . $v['pic_url']);
                }
            }
            M('Weibo')->where(array('uid' => $id))->delete();
        }
        $this->redirect('Admin/User/index');
    }
    
}
